<?php

namespace App\Http\Requests;

use App\Http\Requests\Request;

class NoticeRequest extends Request
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $editRules = [
            'title'    => 'required',
            'content'  => 'required',
            'type'     => 'required|in:1,2,3,4',
            'receiver' => 'required|in:all,single',
            'username' => 'required|numeric',
        ];

        $createRules = [
            'title'    => 'required',
            'content'  => 'required',
            'type'     => 'required|in:1,2,3,4',
            'receiver' => 'required|in:all,single',
            'username' => 'required|numeric',
        ];

//        return array();

        $data = Request::all();
        if ($data['formType'] == 'create' && $data['receiver'] == 'single') {
            return $createRules;
        } elseif ($data['formType'] == 'create' && $data['receiver'] == 'all') {
            array_pop($createRules);

            return $createRules;
        }

        return $editRules;
    }

    public function attributes()
    {
        $attributes = array(
            'title'    => '标题',
            'content'  => '内容',
            'type'     => '类型',
            'receiver' => '接受者人群',
            'username' => '用户手机号',
        );

        return $attributes;
    }

    public function messages()
    {
        $messages = [
            'required'    => ':attribute为必填选项',
            'numeric'     => ':attribute必须为数字',
            'type.in'     => ':attribute必须为后台系统通知、项目、活动、直通车中的一种',
            'receiver.in' => ':attribute必须为所有用户、指定发送给某用户的一种',
        ];

        return $messages;
    }
}
